package com.ruoyi.system.demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：zhanghao
 * @name：PythonTest
 * @Date：2025/8/22 15:41
 */
public class PythonTest {

    public static String runPythonScript(String addresse) {
        String pythonScript = "E:\\gitProject\\RuoYi\\ruoyi-system\\src\\main\\java\\com\\ruoyi\\system\\demo\\address_parse.py";

        List<String> command = new ArrayList<>();
        command.add("python");
        command.add(pythonScript);
        command.add(addresse);

        StringBuffer sb = new StringBuffer();

        try {
//            System.out.println(command);
            ProcessBuilder pb = new ProcessBuilder(command);
            Process process = pb.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));

            String line;
//            System.out.println("Python 脚本开始执行...");
            while ((line = reader.readLine()) != null) {
//                System.out.println("结果=" + line); // JSON 输出
                sb.append(line);
            }

//            int exitCode = process.waitFor();
//            System.out.println("Python exited with code: " + exitCode);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static void main(String[] args) {

        String pythonScript = "E:\\gitProject\\RuoYi\\ruoyi-system\\src\\main\\java\\com\\ruoyi\\system\\demo\\address_parse.py";
        String[] addresses = {"上海市徐汇区虹漕路461号"};
        String address = "，常州市武进华联电控设备股份有限公司，常州市武进区遥观镇留道工业园广电东路89号";

        List<String> command = new ArrayList<>();
        command.add("python");
        command.add(pythonScript);
//        command.addAll(Arrays.asList(addresses));
        command.add(address);


        try {
//            System.out.println(command);
            ProcessBuilder pb = new ProcessBuilder(command);
            Process process = pb.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));

            String line;
            System.out.println("Python 脚本开始执行...");
            while ((line = reader.readLine()) != null) {
                System.out.println("结果=" + line); // JSON 输出
            }

            int exitCode = process.waitFor();
            System.out.println("Python exited with code: " + exitCode);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
